package cn.itcast.orders.mapper;


import cn.itcast.orders.pojo.AddressBook;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface AddressBookMapper {

    /**
     * 根据用户表id查询所有地址信息
     *
     * @param userId
     * @return
     */
    @Select("select * from address_book where user_id = #{userId};")
    List<AddressBook> listByUserId(Long userId);


    /**
     * 新增地址
     *
     * @param addressBook
     */
    void add(AddressBook addressBook);

    /**
     * 根据id查询地址
     *
     * @param id
     * @return
     */
    @Select("select * from address_book where id = #{id};")
    AddressBook getById(Integer id);

    /**
     * 修改地址
     *
     * @param addressBook
     */
    void update(AddressBook addressBook);

    /**
     * 根据id删除地址
     *
     * @param id
     */
    @Delete("delete from address_book where id = #{id}")
    void deleteById(Integer id);

    /**
     * 将当前用户的所有地址设为非默认地址
     *
     * @param userId
     */
    @Update("update address_book set is_default = 0 where user_id = #{userId}")
    void updatesByUserId(Long userId);

    /**
     * 查询默认地址
     *
     * @param userId
     * @return
     */
    AddressBook getDefaultAddressByUserId(Long userId);
}
